-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(SymbolShim): add for polyfill
- refactor SymbolShim away from class usage - removes test that might be brittle when testing in browsers without Symbol impl - adds for function polyfill if not present - improves test looking for Set class
- Loading branch information
Showing
2 changed files
with
90 additions
and
92 deletions.
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,57 +1,56 @@ | ||
import {root} from './root'; | ||
|
||
export class SymbolDefinition { | ||
observable: symbol = null; | ||
iterator: symbol = null; | ||
|
||
private applyObservable(): symbol { | ||
const root = this.root; | ||
|
||
if (!root.Symbol.observable) { | ||
if (typeof root.Symbol.for === 'function') { | ||
root.Symbol.observable = root.Symbol.for('observable'); | ||
} else { | ||
root.Symbol.observable = '@@observable'; | ||
} | ||
} | ||
export function polyfillSymbol(root) { | ||
const Symbol = ensureSymbol(root); | ||
ensureIterator(Symbol, root); | ||
ensureObservable(Symbol); | ||
return Symbol; | ||
} | ||
|
||
return root.Symbol.observable; | ||
export function ensureSymbol(root) { | ||
if (!root.Symbol) { | ||
root.Symbol = { | ||
for: symbolForPolyfill | ||
}; | ||
} | ||
return root.Symbol; | ||
} | ||
|
||
private applyIterator(): symbol { | ||
const root = this.root; | ||
export function symbolForPolyfill(key) { | ||
return '@@' + key; | ||
} | ||
|
||
if (!root.Symbol.iterator) { | ||
if (typeof root.Symbol.for === 'function') { | ||
root.Symbol.iterator = root.Symbol.for('iterator'); | ||
} else if (root.Set && typeof new root.Set()['@@iterator'] === 'function') { | ||
// Bug for mozilla version | ||
root.Symbol.iterator = '@@iterator'; | ||
} else if (root.Map) { | ||
// es6-shim specific logic | ||
let keys = Object.getOwnPropertyNames(root.Map.prototype); | ||
for (let i = 0; i < keys.length; ++i) { | ||
let key = keys[i]; | ||
if (key !== 'entries' && key !== 'size' && root.Map.prototype[key] === root.Map.prototype['entries']) { | ||
root.Symbol.iterator = key; | ||
break; | ||
} | ||
export function ensureIterator(Symbol, root) { | ||
if (!Symbol.iterator) { | ||
if (typeof Symbol.for === 'function') { | ||
Symbol.iterator = Symbol.for('iterator'); | ||
} else if (root.Set && typeof new root.Set()['@@iterator'] === 'function') { | ||
// Bug for mozilla version | ||
Symbol.iterator = '@@iterator'; | ||
} else if (root.Map) { | ||
// es6-shim specific logic | ||
let keys = Object.getOwnPropertyNames(root.Map.prototype); | ||
for (let i = 0; i < keys.length; ++i) { | ||
let key = keys[i]; | ||
if (key !== 'entries' && key !== 'size' && root.Map.prototype[key] === root.Map.prototype['entries']) { | ||
Symbol.iterator = key; | ||
break; | ||
} | ||
} else { | ||
root.Symbol.iterator = '@@iterator'; | ||
} | ||
} else { | ||
Symbol.iterator = '@@iterator'; | ||
} | ||
|
||
return root.Symbol.iterator; | ||
} | ||
} | ||
|
||
constructor(private root: any) { | ||
if (!root.Symbol) { | ||
root.Symbol = {}; | ||
export function ensureObservable(Symbol) { | ||
if (!Symbol.observable) { | ||
if (typeof Symbol.for === 'function') { | ||
Symbol.observable = Symbol.for('observable'); | ||
} else { | ||
Symbol.observable = '@@observable'; | ||
} | ||
|
||
this.observable = this.applyObservable(); | ||
this.iterator = this.applyIterator(); | ||
} | ||
} | ||
export const SymbolShim = new SymbolDefinition(root); | ||
|
||
export const SymbolShim = polyfillSymbol(root); |