Skip to content

Commit

Permalink
Fixed issue where bootstrapping of StateEmitter still happened twice …
Browse files Browse the repository at this point in the history
…when using a self-proxying alias
  • Loading branch information
lVlyke committed Sep 13, 2018
1 parent aedf38b commit f0ec634
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion coverage/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lithiumjs/angular",
"version": "2.0.0-rc.2",
"version": "2.0.0-rc.3",
"description": "A decorator-based library for Angular that enables seamless reactive data binding using RxJS.",
"main": "index.js",
"author": "Mychal Thompson <mychal.r.thompson@gmail.com>",
Expand Down
16 changes: 10 additions & 6 deletions src/state-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,29 +248,33 @@ export namespace StateEmitter {

export function CreateMetadata(target: any, type: EmitterType, metadata: EmitterMetadata.SubjectInfo) {
const initialPropertyDescriptor = Object.getOwnPropertyDescriptor(target, metadata.propertyKey);
const resolveInitialPropertyValue = () => initialPropertyDescriptor ? (initialPropertyDescriptor.value || initialPropertyDescriptor.get()) : undefined;
let bootstrapResult: Observable<any> | Subject<any> = undefined;
let isBootstrapping = false;

function doBootstrap(): Observable<any> | Subject<any> {
// Get the initial value of the property being decorated
const initialPropertyValue: any = initialPropertyDescriptor ? (initialPropertyDescriptor.value || initialPropertyDescriptor.get()) : null;
const initialPropertyValue: any = resolveInitialPropertyValue();

// Check if there's a value set for the property
if (initialPropertyValue) {
// If the value is an Observable, use it for this StateEmitter
if (initialPropertyValue instanceof Observable) {
if (metadata.proxyMode === EmitterMetadata.ProxyMode.None) {
if (!metadata.proxyMode || metadata.proxyMode === EmitterMetadata.ProxyMode.None) {
// Setup a self-proxying alias that will reference the initial value
metadata.proxyMode = EmitterMetadata.ProxyMode.Alias;
metadata.proxyPath = metadata.propertyKey;
}
else {
throw new Error(`[${target.name}]: Unable to create a StateEmitter on property "${metadata.propertyKey}": property cannot have a pre-defined Subject when declaring a proxying StateEmitter.`);
throw new Error(`[${target.name}]: Unable to create a StateEmitter on property "${metadata.propertyKey}": property cannot have a pre-defined observable when declaring a proxying StateEmitter.`);
}
}
else {
console.warn(`Warning: Definition of StateEmitter for ${target.name}.${metadata.propertyKey} is overriding previous value for '${metadata.propertyKey}'.`);
}
}

isBootstrapping = true;
return Bootstrap(this, type);
}

Expand All @@ -286,19 +290,19 @@ export namespace StateEmitter {
Object.defineProperty(target, metadata.propertyKey, {
configurable: true,
get: function () {
if (!bootstrapResult) {
if (!isBootstrapping) {
bootstrapResult = doBootstrap.bind(this)();
}

return bootstrapResult;
return bootstrapResult || resolveInitialPropertyValue();
}
});

// Initialize the facade property to a self-bootstrapper that will initialize the instance's StateEmitters when called
Object.defineProperty(target, type, {
configurable: true,
get: function () {
if (!bootstrapResult) {
if (!isBootstrapping) {
bootstrapResult = doBootstrap.bind(this)();
}

Expand Down

0 comments on commit f0ec634

Please sign in to comment.