-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
0b40635
commit 716e8c0
Showing
9 changed files
with
237 additions
and
11 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
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,55 @@ | ||
//@flow | ||
|
||
import {BaseMiddleware, getLogger} from '@playkit-js/playkit-js'; | ||
import {BasePlugin} from './base-plugin'; | ||
class PluginReadinessMiddleware extends BaseMiddleware { | ||
_plugins: Array<BasePlugin>; | ||
id: string = 'PluginReadinessMiddleware'; | ||
static _logger = getLogger('PluginReadinessMiddleware'); | ||
|
||
constructor(plugins: Array<BasePlugin>) { | ||
super(); | ||
this._plugins = plugins; | ||
PluginReadinessMiddleware._logger.debug('plugins readiness', this._plugins); | ||
} | ||
|
||
/** | ||
* Load middleware handler. | ||
* @param {Function} next - The load handler in the middleware chain. | ||
* @returns {void} | ||
* @memberof PluginReadinessMiddleware | ||
*/ | ||
load(next: Function): void { | ||
this._checkNextSettle(0, next); | ||
} | ||
_checkNextSettle(index: number, next: Function) { | ||
if (index < this._plugins.length) { | ||
this._checkSettle(index, next); | ||
} else { | ||
this.callNext(next); | ||
} | ||
} | ||
|
||
_checkSettle(index: number, next: Function) { | ||
const readyPromise = this._plugins[index].ready ? this._plugins[index].ready : Promise.resolve(); | ||
readyPromise | ||
.then(() => { | ||
PluginReadinessMiddleware._logger.debug(`plugin ${this._plugins[index].name} ready promise resolved`); | ||
this._checkNextSettle(index + 1, next); | ||
}) | ||
.catch(() => { | ||
PluginReadinessMiddleware._logger.debug(`plugin ${this._plugins[index].name} ready promise rejected`); | ||
this._checkNextSettle(index + 1, next); | ||
}); | ||
} | ||
/** | ||
* Play middleware handler. | ||
* @param {Function} next - The play handler in the middleware chain. | ||
* @returns {void} | ||
* @memberof PluginReadinessMiddleware | ||
*/ | ||
play(next: Function): void { | ||
this._checkNextSettle(0, next); | ||
} | ||
} | ||
export {PluginReadinessMiddleware}; |
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
16 changes: 16 additions & 0 deletions
16
test/src/common/plugin/test-plugins/async-reject-plugin.js
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 @@ | ||
import {BasePlugin} from '../../../../../src/common/plugins'; | ||
|
||
export default class AsyncRejectPlugin extends BasePlugin { | ||
static DELAY_ASYNC = 300; | ||
static isValid(): boolean { | ||
return true; | ||
} | ||
|
||
get ready(): Promise<*> { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
reject(); | ||
}, AsyncRejectPlugin.DELAY_ASYNC); | ||
}); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/src/common/plugin/test-plugins/async-resolve-plugin.js
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 @@ | ||
import {BasePlugin} from '../../../../../src/common/plugins'; | ||
|
||
export default class AsyncResolvePlugin extends BasePlugin { | ||
static DELAY_ASYNC = 500; | ||
static isValid(): boolean { | ||
return true; | ||
} | ||
|
||
get ready(): Promise<*> { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, AsyncResolvePlugin.DELAY_ASYNC); | ||
}); | ||
} | ||
} |
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