From 2aad6972647430985b44c1af28028a0a36030af8 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Fri, 29 May 2020 22:08:45 +0300 Subject: [PATCH] Implement icon providers (similar to namespaces) --- .../browser-tests/tests/10-fake-api-test.ts | 70 +++-- .../browser-tests/tests/10-finder-v1-test.ts | 3 + .../browser-tests/tests/10-finder-v2-test.ts | 3 + .../tests/20-renderer-v1-test.ts | 16 +- .../tests/20-renderer-v2-test.ts | 16 +- .../browser-tests/tests/20-scan-dom-test.ts | 2 +- .../tests/21-scan-dom-api-test.ts | 103 ++++--- packages/browser-tests/tests/fake-api.ts | 44 ++- packages/core/src/api/callbacks.ts | 84 ++++-- packages/core/src/api/config.ts | 116 ++++---- packages/core/src/api/index.ts | 197 ++++++++----- packages/core/src/api/modules.ts | 51 ++-- packages/core/src/cache/storage.ts | 13 +- packages/core/src/icon/list.ts | 15 +- packages/core/src/icon/name.ts | 42 ++- packages/core/src/icon/sort.ts | 34 ++- packages/core/src/interfaces/api.ts | 3 +- packages/core/src/interfaces/cache.ts | 2 +- packages/core/src/storage/index.ts | 43 ++- .../core/tests/10-basic/icon-name-test.ts | 74 ++++- packages/core/tests/10-basic/storage-test.ts | 12 +- .../core/tests/30-api/10-callbacks-test.ts | 267 +++++++++++++++--- packages/core/tests/30-api/10-modules-test.ts | 44 ++- packages/core/tests/30-api/20-loading-test.ts | 209 ++++++++------ packages/core/tests/30-cache/10-basic-test.ts | 4 + .../core/tests/30-cache/20-loading-test.ts | 82 +++++- .../core/tests/30-cache/30-saving-test.ts | 45 ++- packages/core/tests/30-cache/fake_cache.ts | 2 +- packages/iconify/src/iconify.ts | 69 +++-- packages/iconify/src/modules/api-jsonp.ts | 20 +- packages/iconify/src/scan.ts | 54 ++-- 31 files changed, 1204 insertions(+), 535 deletions(-) diff --git a/packages/browser-tests/tests/10-fake-api-test.ts b/packages/browser-tests/tests/10-fake-api-test.ts index 4a1c4f6..283cd8d 100644 --- a/packages/browser-tests/tests/10-fake-api-test.ts +++ b/packages/browser-tests/tests/10-fake-api-test.ts @@ -2,14 +2,14 @@ import mocha from 'mocha'; import chai from 'chai'; import { FakeData, setFakeData, prepareQuery, sendQuery } from './fake-api'; import { API } from '@iconify/core/lib/api/'; -import { setAPIModule } from '@iconify/core/lib/api/modules'; +import { setDefaultAPIModule } from '@iconify/core/lib/api/modules'; import { setAPIConfig } from '@iconify/core/lib/api/config'; import { coreModules } from '@iconify/core/lib/modules'; const expect = chai.expect; // Set API -setAPIModule({ +setDefaultAPIModule({ prepare: prepareQuery, send: sendQuery, }); @@ -21,7 +21,8 @@ function nextPrefix(): string { } describe('Testing fake API', () => { - it('Loading results', done => { + it('Loading results', (done) => { + const provider = nextPrefix(); const prefix = nextPrefix(); const data: FakeData = { icons: ['icon1', 'icon2'], @@ -41,24 +42,26 @@ describe('Testing fake API', () => { height: 24, }, }; - setAPIConfig( - { - resources: ['https://api1.local', 'https://api2.local'], - }, - prefix - ); - setFakeData(prefix, data); + setAPIConfig(provider, { + resources: ['https://api1.local', 'https://api2.local'], + }); + setFakeData(provider, prefix, data); // Attempt to load icons API.loadIcons( - [prefix + ':icon1', prefix + ':icon2'], + [ + provider + ':' + prefix + ':icon1', + provider + ':' + prefix + ':icon2', + ], (loaded, missing, pending) => { expect(loaded).to.be.eql([ { + provider, prefix, name: 'icon1', }, { + provider, prefix, name: 'icon2', }, @@ -68,7 +71,8 @@ describe('Testing fake API', () => { ); }); - it('Loading results with delay', done => { + it('Loading results with delay', (done) => { + const provider = nextPrefix(); const prefix = nextPrefix(); const data: FakeData = { icons: ['icon1', 'icon2'], @@ -89,23 +93,22 @@ describe('Testing fake API', () => { height: 24, }, }; - setAPIConfig( - { - resources: ['https://api1.local', 'https://api2.local'], - }, - prefix - ); - setFakeData(prefix, data); + setAPIConfig(provider, { + resources: ['https://api1.local', 'https://api2.local'], + }); + setFakeData(provider, prefix, data); // Attempt to load icons const start = Date.now(); API.loadIcons( [ { + provider, prefix, name: 'icon1', }, { + provider, prefix, name: 'icon2', }, @@ -113,10 +116,12 @@ describe('Testing fake API', () => { (loaded, missing, pending) => { expect(loaded).to.be.eql([ { + provider, prefix, name: 'icon1', }, { + provider, prefix, name: 'icon2', }, @@ -129,7 +134,8 @@ describe('Testing fake API', () => { ); }); - it('Loading partial results', done => { + it('Loading partial results', (done) => { + const provider = nextPrefix(); const prefix = nextPrefix(); const data: FakeData = { icons: ['icon1'], @@ -146,21 +152,21 @@ describe('Testing fake API', () => { height: 24, }, }; - setAPIConfig( - { - resources: ['https://api1.local', 'https://api2.local'], - rotate: 20, - timeout: 100, - limit: 1, - }, - prefix - ); - setFakeData(prefix, data); + setAPIConfig(provider, { + resources: ['https://api1.local', 'https://api2.local'], + rotate: 20, + timeout: 100, + limit: 1, + }); + setFakeData(provider, prefix, data); // Attempt to load icons let counter = 0; API.loadIcons( - [prefix + ':icon1', prefix + ':icon2'], + [ + provider + ':' + prefix + ':icon1', + provider + ':' + prefix + ':icon2', + ], (loaded, missing, pending) => { try { counter++; @@ -169,12 +175,14 @@ describe('Testing fake API', () => { // Loaded icon1 expect(loaded).to.be.eql([ { + provider, prefix, name: 'icon1', }, ]); expect(pending).to.be.eql([ { + provider, prefix, name: 'icon2', }, diff --git a/packages/browser-tests/tests/10-finder-v1-test.ts b/packages/browser-tests/tests/10-finder-v1-test.ts index 7a4b201..1c60b85 100644 --- a/packages/browser-tests/tests/10-finder-v1-test.ts +++ b/packages/browser-tests/tests/10-finder-v1-test.ts @@ -46,6 +46,7 @@ describe('Testing legacy finder', () => { // Test all icons testIcon( { + provider: '', prefix: 'mdi', name: 'home', }, @@ -54,6 +55,7 @@ describe('Testing legacy finder', () => { testIcon( { + provider: '', prefix: 'mdi', name: 'account', }, @@ -62,6 +64,7 @@ describe('Testing legacy finder', () => { testIcon( { + provider: '', prefix: 'ic', name: 'baseline-account', }, diff --git a/packages/browser-tests/tests/10-finder-v2-test.ts b/packages/browser-tests/tests/10-finder-v2-test.ts index 79f3acf..efa45a5 100644 --- a/packages/browser-tests/tests/10-finder-v2-test.ts +++ b/packages/browser-tests/tests/10-finder-v2-test.ts @@ -46,6 +46,7 @@ describe('Testing finder', () => { // Test all icons testIcon( { + provider: '', prefix: 'mdi', name: 'home', }, @@ -54,6 +55,7 @@ describe('Testing finder', () => { testIcon( { + provider: '', prefix: 'mdi', name: 'account', }, @@ -62,6 +64,7 @@ describe('Testing finder', () => { testIcon( { + provider: '', prefix: 'ic', name: 'baseline-account', }, diff --git a/packages/browser-tests/tests/20-renderer-v1-test.ts b/packages/browser-tests/tests/20-renderer-v1-test.ts index 651034f..c2c2ac0 100644 --- a/packages/browser-tests/tests/20-renderer-v1-test.ts +++ b/packages/browser-tests/tests/20-renderer-v1-test.ts @@ -18,7 +18,7 @@ addFinder(iconifyFinder); describe('Testing legacy renderer', () => { // Add mentioned icons to storage - const storage = getStorage('mdi'); + const storage = getStorage('', 'mdi'); addIconSet(storage, { prefix: 'mdi', icons: { @@ -82,6 +82,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account-cash', }); @@ -124,6 +125,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account', }); @@ -167,6 +169,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account-box', }); @@ -214,6 +217,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -255,6 +259,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -299,6 +304,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account', }); @@ -372,6 +378,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -537,6 +544,7 @@ describe('Testing legacy renderer', () => { expect(element).to.not.be.eql(lastElement); // different 'element' and 'name' properties expect(element.name).to.not.be.eql(lastElement.name); expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account', }); @@ -582,6 +590,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -630,6 +639,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -676,6 +686,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -764,6 +775,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -861,6 +873,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -946,6 +959,7 @@ describe('Testing legacy renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); diff --git a/packages/browser-tests/tests/20-renderer-v2-test.ts b/packages/browser-tests/tests/20-renderer-v2-test.ts index f88c3a1..8fc0fe6 100644 --- a/packages/browser-tests/tests/20-renderer-v2-test.ts +++ b/packages/browser-tests/tests/20-renderer-v2-test.ts @@ -18,7 +18,7 @@ addFinder(iconifyFinder); describe('Testing renderer', () => { // Add mentioned icons to storage - const storage = getStorage('mdi'); + const storage = getStorage('', 'mdi'); addIconSet(storage, { prefix: 'mdi', icons: { @@ -82,6 +82,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account-cash', }); @@ -124,6 +125,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account', }); @@ -167,6 +169,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account-box', }); @@ -214,6 +217,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -255,6 +259,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -301,6 +306,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account', }); @@ -374,6 +380,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -541,6 +548,7 @@ describe('Testing renderer', () => { expect(element).to.not.be.eql(lastElement); // different 'element' and 'name' properties expect(element.name).to.not.be.eql(lastElement.name); expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'account', }); @@ -586,6 +594,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -634,6 +643,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -680,6 +690,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -768,6 +779,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -865,6 +877,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); @@ -950,6 +963,7 @@ describe('Testing renderer', () => { // Test element expect(element.name).to.be.eql({ + provider: '', prefix: 'mdi', name: 'home', }); diff --git a/packages/browser-tests/tests/20-scan-dom-test.ts b/packages/browser-tests/tests/20-scan-dom-test.ts index e35277d..742df50 100644 --- a/packages/browser-tests/tests/20-scan-dom-test.ts +++ b/packages/browser-tests/tests/20-scan-dom-test.ts @@ -17,7 +17,7 @@ addFinder(iconifyIconFinder); describe('Scanning DOM', () => { // Add mentioned icons to storage - const storage = getStorage('mdi'); + const storage = getStorage('', 'mdi'); addIconSet(storage, { prefix: 'mdi', icons: { diff --git a/packages/browser-tests/tests/21-scan-dom-api-test.ts b/packages/browser-tests/tests/21-scan-dom-api-test.ts index a4fbb32..eaad3d9 100644 --- a/packages/browser-tests/tests/21-scan-dom-api-test.ts +++ b/packages/browser-tests/tests/21-scan-dom-api-test.ts @@ -5,7 +5,7 @@ import { getNode } from './node'; import { addFinder } from '@iconify/iconify/lib/finder'; import { FakeData, setFakeData, prepareQuery, sendQuery } from './fake-api'; import { API } from '@iconify/core/lib/api/'; -import { setAPIModule } from '@iconify/core/lib/api/modules'; +import { setDefaultAPIModule } from '@iconify/core/lib/api/modules'; import { setAPIConfig } from '@iconify/core/lib/api/config'; import { coreModules } from '@iconify/core/lib/modules'; import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify'; @@ -20,7 +20,7 @@ addFinder(iconifyFinder); addFinder(iconifyIconFinder); // Set API -setAPIModule({ +setDefaultAPIModule({ prepare: prepareQuery, send: sendQuery, }); @@ -33,16 +33,14 @@ function nextPrefix(): string { describe('Scanning DOM with API', () => { it('Scan DOM with API', (done) => { + const provider = nextPrefix(); const prefix1 = nextPrefix(); const prefix2 = nextPrefix(); // Set fake API hosts to make test reliable - setAPIConfig( - { - resources: ['https://api1.local', 'https://api2.local'], - }, - [prefix1, prefix2] - ); + setAPIConfig(provider, { + resources: ['https://api1.local', 'https://api2.local'], + }); // Set icons, load them with various delay const data1: FakeData = { @@ -64,7 +62,7 @@ describe('Scanning DOM with API', () => { height: 24, }, }; - setFakeData(prefix1, data1); + setFakeData(provider, prefix1, data1); const data2: FakeData = { icons: ['account', 'account-box'], @@ -85,24 +83,32 @@ describe('Scanning DOM with API', () => { height: 24, }, }; - setFakeData(prefix2, data2); + setFakeData(provider, prefix2, data2); const node = getNode('scan-dom'); node.innerHTML = '

Testing scanning DOM with API