Skip to content

Commit

Permalink
Fixes for legacy decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
soxtoby committed Nov 12, 2024
1 parent 2b43c09 commit c2387a7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
"program": "${workspaceFolder}/node_modules/wattle/lib/cmd.js",
"args": [ "${relativeFile}", "--show-stacks" ],
"autoAttachChildProcesses": true
},
{
"type": "node",
"request": "launch",
"name": "Run legacy tests",
"program": "${workspaceFolder}/node_modules/wattle/lib/cmd.js",
"args": [ "${relativeFile}", "--show-stacks", "--config", "wattle.config.legacy.js" ],
"autoAttachChildProcesses": true
}
]
}
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
"label": "Run tests",
"type": "shell",
"command": "yarn wattle",
"command": "yarn test",
"options": {
"cwd": "${workspaceFolder}" // Change this if your tests aren't using the top-level node_modules
},
Expand Down
2 changes: 1 addition & 1 deletion packages/event-reduce-react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-reduce-react",
"description": "React integration for event-reduce: state management based on reducing observable events into state",
"version": "0.7.1",
"version": "0.7.2",
"author": "Simon Oxtoby",
"homepage": "https://github.com/soxtoby/event-reduce",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/event-reduce/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-reduce",
"description": "State management based on reducing observable events into state",
"version": "0.7.1",
"version": "0.7.2",
"author": "Simon Oxtoby",
"homepage": "https://github.com/soxtoby/event-reduce",
"repository": {
Expand Down
12 changes: 9 additions & 3 deletions packages/event-reduce/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const ObservableValues = Symbol('ObservableValues');
export function model<Class extends new (...args: any[]) => any>(target: Class): Class {
(target as any)[ModelClassBrand] = true;

let legacyFactories = getOrAddLegacyObservablePropertyFactories(target.prototype);

const className = nameOfFunction(target);
return {
[className]: class extends target {
Expand All @@ -38,7 +40,6 @@ export function model<Class extends new (...args: any[]) => any>(target: Class):
observableValues[key] = defineObservableProperty(this, key, propertyFactories[key]);

// Legacy decorators
let legacyFactories = getOrAddLegacyObservablePropertyFactories(Object.getPrototypeOf(this));
for (let key of Object.getOwnPropertyNames(legacyFactories)) // Super class could have its own value factories
observableValues[key] = defineObservableProperty(this, key, legacyFactories[key]);
} finally {
Expand Down Expand Up @@ -118,7 +119,12 @@ export function reduced<Value, Model extends object>(...args:
let getter = property.get;
getOrAddLegacyObservablePropertyFactories(prototype)[key] = instance => getterReduction(instance, key, getter);
return {
get(this: object) { throw new MissingModelDecoratorError(this, key); },
get(this: object) {
let observableValue = getObservableValue(this, key as string);
if (!observableValue)
throw new MissingModelDecoratorError(this, key);
return observableValue.value;
},
enumerable: true,
configurable: true
};
Expand Down Expand Up @@ -228,7 +234,7 @@ export function state<Model, Value>(...args:
if (argsAre(args, isObject, isString)) {
// Legacy decorator
let [instance, key] = args;
addStateProp(Object.getPrototypeOf(instance), key);
addStateProp(instance.constructor.prototype, key);
} else if (argsAre(args, isUndefined, isClassFieldDecoratorContext)) {
// Field decorator
let [_, context] = args;
Expand Down

0 comments on commit c2387a7

Please sign in to comment.