This repository has been archived by the owner on Jan 12, 2020. It is now read-only.
refactor(tracked): minimally invasive stage 2 decorators support #22
Merged
rwjblue
merged 20 commits into
rwjblue:master
from
buschtoens:refactor/stage-2-decorators
Jan 10, 2019
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bce9b0f
chore(deps): upgrade decorator related dependencies
buschtoens 3fd3ea9
refactor(tracked): minimally invasive stage 2 decorators support
buschtoens fce9928
chore(deps): use tilde range for `@types/ember`
mike-north 009d874
chore(deps): use tilde range for `@types/ember-qunit`
mike-north 6bea49f
chore(deps): use tilde range for `@types/ember__test-helpers`
mike-north b559e7e
chore(deps): use tilde range for `@types/ember-test-helpers`
mike-north 80e9735
test: run tests for ember-cli-typescript@1
buschtoens 666de51
fix(tracked): handle missing desc for unitialized fields in e-c-ts@1
buschtoens e345174
chore(deps): update yarn.lock
buschtoens e47eb71
chore(deps): remove `ember-cli-typescript-blueprints`
buschtoens 51bee13
chore(tracked): return void
buschtoens 12d26a2
chore(deps): upgrade @types/ember
buschtoens ec55d1c
refactor(tracked): fix type errors with `addObserver`
buschtoens c5a0dda
chore(deps): upgrade ember-cli-typescript and ember-cli-babel
buschtoens 4ea3b97
chore(deps): downgrade typescript@^2.9.2
buschtoens 6a614f3
chore(deps): temporarily use pre-release of @e-d/utils
buschtoens d0f93f6
chore(deps): upgrade to @e-d v4.0.0
buschtoens e72fcfe
chore(types): add ember-compatibility-helpers
buschtoens 158873b
chore(types): add types for new `setComponentManager`
buschtoens 048c4fc
chore(types): remove unnecessary types
buschtoens File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,16 +1,17 @@ | ||||||||||||||||||
import Ember from 'ember'; | ||||||||||||||||||
import { assert } from '@ember/debug'; | ||||||||||||||||||
import { DEBUG } from '@glimmer/env'; | ||||||||||||||||||
import { notifyPropertyChange } from '@ember/object'; | ||||||||||||||||||
import { addObserver } from '@ember/object/observers'; | ||||||||||||||||||
import { decoratorWithParams } from '@ember-decorators/utils/decorator'; | ||||||||||||||||||
|
||||||||||||||||||
function setupObservers(instance: object, dependentKeys: string[], notifyMethod: (() => void)) { | ||||||||||||||||||
function setupObservers<O extends object>(instance: O, dependentKeys: (keyof O)[], notifyMethod: (() => void)) { | ||||||||||||||||||
for (let i = 0; i < dependentKeys.length; i++) { | ||||||||||||||||||
let dependentKey = dependentKeys[i]; | ||||||||||||||||||
addObserver(instance, dependentKey, instance, notifyMethod); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
function descriptorForTrackedComputedProperty(_target: any, key: string | symbol, desc: PropertyDescriptor, dependencies?: string[]) { | ||||||||||||||||||
function descriptorForTrackedComputedProperty(key: string | symbol, desc: PropertyDescriptor, dependencies?: string[]) { | ||||||||||||||||||
// TODO: really should use WeakSet here, but that isn't available on IE11 | ||||||||||||||||||
const OBSERVERS_SETUP = new WeakMap(); | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -36,15 +37,15 @@ function descriptorForTrackedComputedProperty(_target: any, key: string | symbol | |||||||||||||||||
// will be bound to the instance when invoked | ||||||||||||||||||
function notify(this: object) { | ||||||||||||||||||
if (typeof key === 'string') { | ||||||||||||||||||
Ember.notifyPropertyChange(this, key); | ||||||||||||||||||
notifyPropertyChange(this, key); | ||||||||||||||||||
} else if (DEBUG) { | ||||||||||||||||||
throw new Error(`@tracked - unsupported property type ${String(key)}`); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
desc.get = function() { | ||||||||||||||||||
if (!OBSERVERS_SETUP.has(this) && Array.isArray(dependencies)) { | ||||||||||||||||||
setupObservers(this, dependencies, notify); | ||||||||||||||||||
setupObservers<any>(this, dependencies, notify); | ||||||||||||||||||
} | ||||||||||||||||||
OBSERVERS_SETUP.set(this, true); | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -54,7 +55,7 @@ function descriptorForTrackedComputedProperty(_target: any, key: string | symbol | |||||||||||||||||
if (setterProvided) { | ||||||||||||||||||
desc.set = function(value) { | ||||||||||||||||||
if (typeof key === 'string') { | ||||||||||||||||||
Ember.notifyPropertyChange(this, key); | ||||||||||||||||||
notifyPropertyChange(this, key); | ||||||||||||||||||
setterProvided.call(this, value); | ||||||||||||||||||
} else if (DEBUG) { | ||||||||||||||||||
throw new Error(`@tracked - unsupported property type ${String(key)}`); | ||||||||||||||||||
|
@@ -65,11 +66,7 @@ function descriptorForTrackedComputedProperty(_target: any, key: string | symbol | |||||||||||||||||
return desc; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
function installTrackedProperty(_target: object, key: string | symbol, descriptor?: PropertyDescriptor & { initializer: (() => void)}): PropertyDescriptor { | ||||||||||||||||||
// only happens in babel, never in TS (Sept 2018) | ||||||||||||||||||
// TODO check for whether initializer is a function | ||||||||||||||||||
const initializer = descriptor && descriptor.initializer; | ||||||||||||||||||
|
||||||||||||||||||
function installTrackedProperty(key: string | symbol, descriptor?: PropertyDescriptor, initializer?: () => any): PropertyDescriptor { | ||||||||||||||||||
let values = new WeakMap(); | ||||||||||||||||||
|
||||||||||||||||||
let get; | ||||||||||||||||||
|
@@ -90,54 +87,45 @@ function installTrackedProperty(_target: object, key: string | symbol, descripto | |||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
return { | ||||||||||||||||||
configurable: true, | ||||||||||||||||||
// TODO: correcting a misspelling caused chrome to error | ||||||||||||||||||
// writable: true, | ||||||||||||||||||
|
||||||||||||||||||
configurable: descriptor ? descriptor.configurable : true, | ||||||||||||||||||
enumerable: descriptor ? descriptor.enumerable : true, | ||||||||||||||||||
get, | ||||||||||||||||||
set(value) { | ||||||||||||||||||
if (typeof key === 'string') { | ||||||||||||||||||
values.set(this, value); | ||||||||||||||||||
Ember.notifyPropertyChange(this, key); | ||||||||||||||||||
notifyPropertyChange(this, key); | ||||||||||||||||||
} else if (DEBUG) { | ||||||||||||||||||
throw new Error(`@tracked - unsupported property type ${String(key)}`); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
}; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
function _tracked(target: object, key: string | symbol, descriptor?: PropertyDescriptor, dependencies?: string[]): PropertyDescriptor { | ||||||||||||||||||
// descriptor is undefined for typescript class fields | ||||||||||||||||||
if (descriptor === undefined || 'initializer' in descriptor) { | ||||||||||||||||||
return installTrackedProperty(target, key, descriptor); | ||||||||||||||||||
function _tracked( | ||||||||||||||||||
key: string | symbol, | ||||||||||||||||||
descriptor?: PropertyDescriptor, | ||||||||||||||||||
initializer?: () => any, | ||||||||||||||||||
dependencies?: string[] | ||||||||||||||||||
): PropertyDescriptor { | ||||||||||||||||||
if (!descriptor || typeof descriptor.get !== 'function' && typeof descriptor.set !== 'function') { | ||||||||||||||||||
return installTrackedProperty(key, descriptor, initializer); | ||||||||||||||||||
} else { | ||||||||||||||||||
return descriptorForTrackedComputedProperty(target, key, descriptor, dependencies); | ||||||||||||||||||
return descriptorForTrackedComputedProperty(key, descriptor, dependencies); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
// type CompatiblePropertyDecorator = (target: object, key: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||||||||||||||||||
|
||||||||||||||||||
// @tracked | ||||||||||||||||||
// TODO: replace return w/ PropertyDescriptor once TS gets their decorator act together | ||||||||||||||||||
export function tracked(target: object, propertyKey: string | symbol, descriptor?: PropertyDescriptor): any; | ||||||||||||||||||
// @tracked('foo', 'bar') | ||||||||||||||||||
// TODO: replace return w/ CompatiblePropertyDecorator | ||||||||||||||||||
export function tracked(...args: string[]): any; | ||||||||||||||||||
// TODO: replace return w/ CompatiblePropertyDecorator | PropertyDescriptor | ||||||||||||||||||
export function tracked( | ||||||||||||||||||
targetOrArgs: (string | object), | ||||||||||||||||||
secondArg: string | symbol, | ||||||||||||||||||
descriptorOrString: string | PropertyDescriptor | undefined, | ||||||||||||||||||
...rest: string[]): any | ||||||||||||||||||
{ | ||||||||||||||||||
// if called for `@tracked('foo')` | ||||||||||||||||||
if (typeof targetOrArgs === 'string') { // @tracked('foo', 'bar') | ||||||||||||||||||
const args = [targetOrArgs, secondArg as string, descriptorOrString as string, ...rest]; | ||||||||||||||||||
return function(target: object, key: string | symbol, descriptor: PropertyDescriptor) { | ||||||||||||||||||
return _tracked(target, key, descriptor, args); | ||||||||||||||||||
} | ||||||||||||||||||
} else { // @tracked | ||||||||||||||||||
return _tracked(targetOrArgs, secondArg, descriptorOrString as PropertyDescriptor | undefined); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
type TSDecorator = (target: object, propertyKey: string | symbol, descriptor?: PropertyDescriptor) => void; | ||||||||||||||||||
type TrackedDecorator = TSDecorator & ((...args: string[]) => TSDecorator); | ||||||||||||||||||
|
||||||||||||||||||
export const tracked: TrackedDecorator = decoratorWithParams((desc, params = []) => { | ||||||||||||||||||
assert(`@tracked - Can only be used on class fields.`, desc.kind === 'field' || desc.kind === 'method'); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you wanna be really correct.
Suggested change
|
||||||||||||||||||
const descriptor = _tracked(desc.key, desc.descriptor, desc.initializer, params); | ||||||||||||||||||
|
||||||||||||||||||
return { | ||||||||||||||||||
...desc, | ||||||||||||||||||
descriptor, | ||||||||||||||||||
kind: 'method', | ||||||||||||||||||
initializer: undefined | ||||||||||||||||||
}; | ||||||||||||||||||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
type MemberDescriptor = { | ||
key: string; | ||
kind: 'class' | 'method' | 'field' | 'initializer'; | ||
elements?: MemberDescriptor[]; | ||
key: string; | ||
placement?: 'prototype' | 'static' | 'own'; | ||
extras?: MemberDescriptor[]; | ||
initializer?: () => any; | ||
descriptor?: PropertyDescriptor; | ||
}; | ||
|
||
export function decoratorWithParams( | ||
fn: (desc: MemberDescriptor, params?: any[]) => MemberDescriptor | ||
): any; | ||
buschtoens marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export function gte(version: string): boolean; | ||
export function lte(version: string): boolean; | ||
export const HAS_UNDERSCORE_ACTIONS: boolean; | ||
buschtoens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export const HAS_MODERN_FACTORY_INJECTIONS: boolean; | ||
export const HAS_DESCRIPTOR_TRAP: boolean; | ||
export const HAS_NATIVE_COMPUTED_GETTERS: boolean; | ||
export const IS_GLIMMER_2: boolean; | ||
export const IS_RECORD_DATA: boolean; | ||
export const SUPPORTS_FACTORY_FOR: boolean; | ||
export const SUPPORTS_GET_OWNER: boolean; | ||
export const SUPPORTS_SET_OWNER: boolean; | ||
export const SUPPORTS_NEW_COMPUTED: boolean; | ||
export const SUPPORTS_INVERSE_BLOCK: boolean; | ||
export const SUPPORTS_CLOSURE_ACTIONS: boolean; | ||
export const SUPPORTS_UNIQ_BY_COMPUTED: boolean; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW this is intentional. I fixed the same problem in this PR with a detailed analysis: salsify/ember-css-modules#118